﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;

namespace DynamicProxy
{
    /// <summary>
    /// 动态代理容器
    /// </summary>
    public class DynamicProxyContain
    {
        /// <summary>
        /// 建立一个动态代理实例
        /// </summary>
        /// <typeparam name="I"></typeparam>
        /// <typeparam name="TClass"></typeparam>
        /// <param name="handler"></param>
        /// <returns></returns>
        public static TInterface Instance<TInterface, TClass>(Castle.DynamicProxy.IInterceptor[] handler)
            where TClass : class,TInterface
        {
            var proxyGenerator = new ProxyGenerator();
            return (TInterface)proxyGenerator.CreateClassProxy<TClass>(handler);
        }
        /// <summary>
        /// 建立一个动态代理实例
        /// </summary>
        /// <typeparam name="I"></typeparam>
        /// <typeparam name="TClass"></typeparam>
        /// <param name="handler"></param>
        /// <returns></returns>
        public static TInterface Instance<TInterface, TClass>(Castle.DynamicProxy.IInterceptor handler)
            where TClass : class,TInterface
        {
            return Instance<TInterface, TClass>(new Castle.DynamicProxy.IInterceptor[] { handler });
        }
    }
}
